package com.ltkj.coupon.controller;

import com.ltkj.common.core.utils.poi.ExcelUtil;
import com.ltkj.common.core.web.controller.BaseController;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.core.web.page.TableDataInfo;
import com.ltkj.common.log.annotation.Log;
import com.ltkj.common.log.enums.BusinessType;
import com.ltkj.common.security.annotation.RequiresPermissions;
import com.ltkj.coupon.domain.TCouponUser;
import com.ltkj.coupon.domain.dto.CouponUserPageQueryDTO;
import com.ltkj.coupon.domain.dto.TCouponUserAddDTO;
import com.ltkj.coupon.domain.vo.TCouponUserPageVo;
import com.ltkj.coupon.service.ITCouponUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 用户优惠券Controller
 *
 * @author ruoyi
 * @date 2023-09-21
 */
@Api(tags = "用户优惠券")
@RestController
@RequestMapping("/usercoupon")
public class TCouponUserController extends BaseController {
    @Autowired
    private ITCouponUserService tCouponUserService;

    // /**
    //  * 查询用户优惠券列表
    //  */
    // @ApiOperation("查询用户优惠券列表")
    // @RequiresPermissions("usercoupon:usercoupon:list")
    // @GetMapping("/list")
    // public TableDataInfo list(TCouponUser tCouponUser) {
    //     startPage();
    //     tCouponUser.setIsDel("0");
    //     List<TCouponUser> list = tCouponUserService.selectTCouponUserList(tCouponUser);
    //     return getDataTable(list);
    // }

    /**
     * 查询用户优惠券列表
     */
    @ApiOperation("查询用户优惠券列表")
    @RequiresPermissions("usercoupon:usercoupon:list")
    @GetMapping("/list")
    public TableDataInfo list(CouponUserPageQueryDTO queryDTO) {
        startPage();
        List<TCouponUserPageVo> list = tCouponUserService.selectTCouponUserPage(queryDTO);
        return getDataTable(list);
    }

    /**
     * 导出用户优惠券列表
     */
    @ApiOperation("导出用户优惠券列表")
    @RequiresPermissions("usercoupon:usercoupon:export")
    @Log(title = "用户优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCouponUser tCouponUser) {
        List<TCouponUser> list = tCouponUserService.selectTCouponUserList(tCouponUser);
        ExcelUtil<TCouponUser> util = new ExcelUtil<TCouponUser>(TCouponUser.class);
        util.exportExcel(response, list, "用户优惠券数据");
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<TCouponUserAddDTO> util = new ExcelUtil<TCouponUserAddDTO>(TCouponUserAddDTO.class);
        util.importTemplateExcel(response, "优惠券数据");
    }

    /**
     * 获取用户优惠券详细信息
     */
    @ApiOperation("获取用户优惠券详细信息")
    @RequiresPermissions("usercoupon:usercoupon:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tCouponUserService.selectTCouponUserById(id));
    }

    /**
     * 新增用户优惠券
     */
    @ApiOperation("新增用户优惠券批量")
    @RequiresPermissions("usercoupon:usercoupon:import")
    @Log(title = "用户优惠券", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(Long couponTempId, MultipartFile file, boolean updateSupport) throws Exception {

        ExcelUtil<TCouponUserAddDTO> util = new ExcelUtil<TCouponUserAddDTO>(TCouponUserAddDTO.class);
        List<TCouponUserAddDTO> tCouponUserAddDTOList = null;

        tCouponUserAddDTOList = util.importExcel(file.getInputStream());
        String message  = tCouponUserService.insertTCouponUserList(tCouponUserAddDTOList, couponTempId);
        return success(message);
    }

    /**
     * 新增用户优惠券
     */
    @ApiOperation("新增用户优惠券")
    @RequiresPermissions("usercoupon:usercoupon:add")
    @Log(title = "用户优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCouponUserAddDTO tCouponUserAddDTO) {
        return toAjax(tCouponUserService.insertTCouponUserV2(tCouponUserAddDTO));
    }

    /**
     * 修改用户优惠券
     */
    @ApiOperation("修改用户优惠券")
    @RequiresPermissions("usercoupon:usercoupon:edit")
    @Log(title = "用户优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCouponUser tCouponUser) {
        return toAjax(tCouponUserService.updateTCouponUser(tCouponUser));
    }

    /**
     * 作废用户优惠券
     */
    @ApiOperation("作废用户优惠券")
    @RequiresPermissions("usercoupon:usercoupon:cancel")
    @Log(title = "用户优惠券", businessType = BusinessType.CANCLE)
    @GetMapping("/cancle/{id}")
    public AjaxResult cancel(@PathVariable("id") Long id) {
        return toAjax(tCouponUserService.cancel(id));
    }

    /**
     * /**
     * 删除用户优惠券
     */
    @ApiOperation("删除用户优惠券")
    @RequiresPermissions("usercoupon:usercoupon:remove")
    @Log(title = "用户优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tCouponUserService.deleteTCouponUserByIds(ids));
    }
}
